<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Console\Commands;

use App\Enums\AdminCheckEnum;
use App\Enums\AdminLetterIsReadEnum;
use App\Enums\CheckStatusEnum;
use App\Enums\UserIdentityEnum;
use App\Models\AdminLetter;
use App\Models\AdminRoleAction;
use App\Models\AdminUser;
use App\Models\CheckUserProfile;
use App\Models\Company;
use App\Models\House;
use App\Models\HouserScore;
use App\Models\PassCard;
use App\Models\Recruitment;
use App\Models\SearchNotice;
use Illuminate\Console\Command;

class CensusAdminCheckTask extends Command
{
    const CHECK_FLOW_USER_UUID = '82957804-e972-11eb-ae34-0242ac110003';

    const CHECK_NOT_FLOW_USER_UUID = '82957804-e972-11eb-ae34-0242ac110003';

    const CHECK_SEARCH_NOTICE_UUID = 'e90427a5-e972-11eb-ae34-0242ac110003';

    const CHECK_HOUSE_UUID = 'd6040b0f-e972-11eb-ae34-0242ac110003';

    const CHECK_HOUSE_SCORE_UUID = 'ef92d056-ef7a-11eb-ae34-0242ac110003';

    const CHECK_COMPANY_UUID = '8454e073-f8e0-11eb-9783-0242ac110003';

    const CHECK_PASS_CARD_UUID = '63e8a8cd-e971-11eb-ae34-0242ac110003';

    const CHECK_RECRUITMENT_UUID = '08962dcd-0716-11ec-9783-0242ac110003';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'census:admin_check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计还没有处理的审核';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 统计审核类型
        $checkTypes = AdminCheckEnum::toArray();
        foreach ($checkTypes as $checkType => $typeName) {
            $notChecknumber = $this->countNotCheckNumer($checkType);
            if ($notChecknumber < 1) {
                continue;
            }
            $content = sprintf('%s 已堆积 %s 条', AdminCheckEnum::desc($checkType), $notChecknumber);
            $adminUserIds = $this->findAdminUserIdsFromCheckType($checkType);
            foreach ($adminUserIds as $adminUserId) {
                AdminLetter::query()->create([
                    'admin_user_id' => $adminUserId,
                    'content' => $content,
                    'push_time' => time(),
                    'type' => $checkType,
                    'is_read' => AdminLetterIsReadEnum::NOT_READ,
                ]);
            }
        }
        return 0;
    }

    private function countNotCheckNumer(int $checkType)
    {
        if ($checkType == AdminCheckEnum::CHECK_FLOW_USER) {
            return CheckUserProfile::query()
                ->where('identity', UserIdentityEnum::FLOW_USER)
                ->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_NOT_FLOW_USER) {
            return CheckUserProfile::query()
                ->whereIn('identity', [UserIdentityEnum::HOUSER, UserIdentityEnum::NORMAL])
                ->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_SEARCH_NOTICE) {
            return SearchNotice::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_HOUSE) {
            return House::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_COMPANY) {
            return Company::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_PASS_CARD) {
            return PassCard::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_HOUSE_SCORE) {
            return HouserScore::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        if ($checkType == AdminCheckEnum::CHECK_RECRUITMENT) {
            return Recruitment::query()->where('check_status', CheckStatusEnum::CHECK_ING)->count();
        }
        return 0;
    }

    // 获取要接受对应信息的管理员 user_id
    private function findAdminUserIdsFromCheckType(int $checkType)
    {
        $builder = AdminRoleAction::query()->select('admin_role_id');
        if ($checkType == AdminCheckEnum::CHECK_FLOW_USER) {
            $builder->where('admin_role_id', self::CHECK_FLOW_USER_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_NOT_FLOW_USER) {
            $builder->where('admin_role_id', self::CHECK_NOT_FLOW_USER_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_SEARCH_NOTICE) {
            $builder->where('admin_role_id', self::CHECK_SEARCH_NOTICE_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_HOUSE) {
            $builder->where('admin_role_id', self::CHECK_HOUSE_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_COMPANY) {
            $builder->where('admin_role_id', self::CHECK_COMPANY_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_PASS_CARD) {
            $builder->where('admin_role_id', self::CHECK_PASS_CARD_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_HOUSE_SCORE) {
            $builder->where('admin_role_id', self::CHECK_HOUSE_SCORE_UUID);
        }
        if ($checkType == AdminCheckEnum::CHECK_RECRUITMENT) {
            $builder->where('admin_role_id', self::CHECK_RECRUITMENT_UUID);
        }
        $adminRoleIds = $builder->get()->pluck('admin_role_id')->toArray();
        // 把超管放进去 超管的 admin_role_id 为 1
        array_push($adminRoleIds, 1);
        return AdminUser::query()->whereIn('admin_role_id', $adminRoleIds)
            ->select('id')->get()->pluck('id')->toArray();
    }
}
